<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of LearnController
 *
 * @author gc
 */
class LearnController extends AdminController
{

    protected $student = '';

    public function _initialize()
    {
        parent::_initialize();
        $sid = I("sid");
        if (!empty($sid)) {
            session('sid', $sid);
        } else {
            $sid = session('sid');
        }
        $this->student = D("Student")->getStudentById($sid);
    }

    public function index()
    {
        $map['sid'] = $this->student['id'];
        $map['status'] = array('eq', 1);

        $list = $this->lists('Learn', $map, 'create_time desc');
        $this->assign('_list', $list);

        $this->meta_title = $this->student['name'] . ' 学情评估记录';

        $this->display();
    }

    public function add()
    {
        $id = I("id");
        $meta_title = "{$this->student['name']} 新增学情评估";
        if (!empty($id)) {
            $map["id"] = $id;
            $data = M("Learn")->where($map)->find();
            $this->assign("data", $data);
            $meta_title = "{$this->student['name']} 学情评估查看";

            if (1 == $data['is_complete']) {
                $where['learn_id'] = $data['id'];
                $list = M("Learn_log")->where($where)->select();
                $this->assign('_list', $list);
            }
        }

        //课程列表
        $courselist = D("Course")->getAllCourse();
        $this->assign("courselist", $courselist);

        $this->meta_title = $meta_title;
        $this->display();
    }

    public function show()
    {
        $this->add();
    }

    public function delete()
    {
        $ids = I("id");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => -1,
            'update_time' => time()
        ];

        $s = M("Learn")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

    //开始评估答题
    public function ajaxAnswer()
    {
        $data['course_id'] = I("course_id");

        if (!empty($data['course_id'])) {
            $data['sid'] = $this->student['id'];
            $data['admin_id'] = UID;
            $data['create_time'] = $data['update_time'] = time();
            $learn_id = M("Learn")->add($data);
            $learn_id ? '' : $this->ajaxReturn(['status' => 0]);
        } else {
            $this->ajaxReturn(['status' => 0]);
        }

        $map['course_id'] = $data['course_id'];
        $map['status'] = 1;
        $question = M("Course_assessmont")->where($map)->order('gorder asc')->select();
        $this->assign('question', $question);

        $this->ajaxReturn(['status' => 1, 'learn_id' => $learn_id, 'question' => $question]);
    }

    //评估入库
    public function saveAnswer()
    {
        $learn_id = I("learn_id");
        $qid = I("qid");
        $qtitle = I("qtitle");
        $answer = I("answer");
        $data = [];
        foreach ($qid as $k => $v) {
            $temp = [
                'learn_id' => $learn_id,
                'qid' => $v,
                'qtitle' => $qtitle[$k],
                'answer' => $answer[$k],
                'create_time' => time()
            ];
            array_push($data, $temp);
        }
        if (count($data) > 0) {
            $s = M("Learn_log")->addAll($data);
        }

        if ($s) {
            M("Learn")->where('id=' . $learn_id)->setField('is_complete', 1);
        }

        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

}
